package net.jgrm.jetcache.monitor;

import lombok.extern.slf4j.Slf4j;
import net.jgrm.jetcache.config.JetCacheProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * JetCache 监控服务
 * 提供缓存统计和监控信息
 *
 * @author jgrm
 */
@Slf4j
@RestController
@RequestMapping("/jetcache/stats")
public class JetCacheMonitorService {

    private final JetCacheProperties properties;

    public JetCacheMonitorService(JetCacheProperties properties) {
        this.properties = properties;
    }

    /**
     * 获取所有缓存统计信息
     */
    @GetMapping
    public Map<String, Object> getCacheStats() {
        Map<String, Object> result = new HashMap<>();

        try {
            // 基本配置信息
            result.put("config", getConfigInfo());

            // 系统状态
            result.put("status", getSystemStatus());

            result.put("cacheManagerAvailable", true);

        } catch (Exception e) {
            log.error("Error collecting cache stats", e);
            result.put("error", e.getMessage());
        }

        return result;
    }

    /**
     * 获取简化统计
     */
    @GetMapping("/simple")
    public Map<String, Object> getSimpleStats() {
        Map<String, Object> result = new HashMap<>();

        try {
            result.put("enabled", properties.isEnabled());
            result.put("cacheType", properties.getCacheType());
            result.put("timestamp", System.currentTimeMillis());
            result.put("status", "ok");

        } catch (Exception e) {
            log.error("Error collecting simple stats", e);
            result.put("error", e.getMessage());
            result.put("status", "error");
        }

        return result;
    }

    /**
     * 获取配置信息
     */
    private Map<String, Object> getConfigInfo() {
        Map<String, Object> config = new HashMap<>();
        config.put("enabled", properties.isEnabled());
        config.put("cacheType", properties.getCacheType());

        // 本地缓存配置
        Map<String, Object> localConfig = new HashMap<>();
        localConfig.put("defaultExpireInMillis", properties.getLocal().getDefaultExpireInMillis().toMillis());
        localConfig.put("limit", properties.getLocal().getLimit());
        config.put("local", localConfig);

        // 远程缓存配置
        Map<String, Object> remoteConfig = new HashMap<>();
        remoteConfig.put("defaultExpireInMillis", properties.getRemote().getDefaultExpireInMillis().toMillis());
        remoteConfig.put("keyPrefix", properties.getRemote().getKeyPrefix());
        remoteConfig.put("broadcastChannel", properties.getRemote().getBroadcastChannel());
        config.put("remote", remoteConfig);

        return config;
    }

    /**
     * 获取系统状态
     */
    private Map<String, Object> getSystemStatus() {
        Map<String, Object> status = new HashMap<>();

        // JVM内存信息
        Runtime runtime = Runtime.getRuntime();
        status.put("maxMemory", runtime.maxMemory());
        status.put("totalMemory", runtime.totalMemory());
        status.put("freeMemory", runtime.freeMemory());
        status.put("usedMemory", runtime.totalMemory() - runtime.freeMemory());

        // 系统时间
        status.put("currentTime", System.currentTimeMillis());

        return status;
    }
}